<?php

class FunctionController extends CController
{

	public function uploadImage($file, $directory) {
		Yii2Debug::dump($file);
		$date = date('YmdHis');
		$i = rand(1, 100000);
		$ext = end(explode(".", $file['name']));
		move_uploaded_file(
			$file['tmp_name'],
			YiiBase::getPathOfAlias('webroot') .'/uploads/'. $directory .'/x'. $i .'d'.$date . '.' . $ext);
		return '/uploads/'. $directory .'/x'. $i .'d'. $date .'.'. $ext;
	}
	public function actionUploadIcon($f) {
		foreach ($_FILES as $file) {
			if($file['tmp_name']) {
				if (is_array($file['name'])) {
						for ($i=0;$i<count($file['name']); $i++) {
							$img['name'] = $file['name'][$i];
							$img['tmp_name'] = $file['tmp_name'][$i];
						echo str_replace(array("\r", "\n", " "), '', $this->uploadImage($img, $f));
						}
				} else {
					echo str_replace(array("\r", "\n", " "), '', $this->uploadImage($file, $f));
				}
			}
		}
	}
	public function uploadImages($file, $directory) {
		Yii2Debug::dump($file);
		$date = date('YmdHis');
		$i = rand(1, 100000);
		$ext = end(explode(".", $file['name']));
		move_uploaded_file(
			$file['tmp_name'],
			YiiBase::getPathOfAlias('webroot') .'/uploads/'. $directory .'/x'. $i .'d'.$date . '.' . $ext);
		return '/uploads/'. $directory .'/x'. $i .'d'. $date .'.'. $ext;
	}
	public function actionUploadIcons($f) {
		foreach ($_FILES as $file) {
			if($file['tmp_name']) {
				for ($i=0;$i<count($file['name']); $i++) {
					$img['name'] = $file['name'][$i];
					$img['tmp_name'] = $file['tmp_name'][$i];
					$data[$img['name']]= str_replace(array("\r", "\n", " "), '', $this->uploadImages($img, $f));
				}
				echo json_encode($data);
			}
		}
	}
	public function actionDeletedImage($img)
	{
		if(file_exists(YiiBase::getPathOfAlias('webroot').$img)){
			unlink(YiiBase::getPathOfAlias('webroot').$img);
		}
	}

}